package com.yxjx.duoxue.customview;

import com.yxjx.duoxue.R;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

public class DownTimeCounter extends LinearLayout {
	
	private static final int NOT_STARTED = 1;
	private static final int IN_PROCESS = 2;
	private static final int ALREAY_FINISHED = 3;
	
	private static final int MSG_REFRESH_UI = 0;
	
	private long startTime;
	private long endTime;

	private int dayCount = 0;
	private int hourCount = 0;
	private int minuteCount = 0;
	private int secondCount = 0;
	private TextView headText;
	private TextView hourText;
	private TextView minuteText;
	private TextView secondText;
	
	public DownTimeCounter(Context context, AttributeSet attrs) {
		super(context, attrs);
		LayoutInflater.from(context).inflate(
				R.layout.segment_down_time_counter, this, true);

		headText = (TextView) findViewById(R.id.head_text);
		hourText = (TextView) findViewById(R.id.hour);
		minuteText = (TextView) findViewById(R.id.minute);
		secondText = (TextView) findViewById(R.id.second);
		
		init();

	}

	/**
	 * 指定起始时间
	 * @param startTime
	 * @param endTime
	 */
	public void setStartEndTime(long startTime, long endTime) {
		this.startTime = startTime;
		this.endTime = endTime;
		mHandler.sendEmptyMessage(MSG_REFRESH_UI);
	}

	/**
	 * 根据时间刷新UI
	 */
	private void refreshUI() {
		if (startTime > endTime) {
			return;
		}
		long nowTime = System.currentTimeMillis();
		if (nowTime < startTime) {
			// 未开始
			long diff = startTime - nowTime;
			updateCounts(diff, NOT_STARTED);

		} else if (nowTime >= startTime && nowTime <= endTime) {
			// 抢购中
			long diff = endTime - nowTime;
			updateCounts(diff, IN_PROCESS);

		} else {
			// 活动结束
			updateCounts(0, ALREAY_FINISHED);

		}
	}

	Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_REFRESH_UI:
				removeMessages(MSG_REFRESH_UI);
				refreshUI();
				sendMessageDelayed(obtainMessage(MSG_REFRESH_UI), 1000);
				break;
			default:
				break;
			}
		};
	};

	private String int2Str(int count) {
		if (count >= 60 || count < 0) {
			return "" + count;
		}
		return String.format("%02d", count);
	}

	/**
	 * 
	 * @param diff
	 * @param flag
	 *            NOT_STARTED, IN_PROCESS, ALREAY_FINISHED.
	 */
	private void updateCounts(long diff, int flag) {
		diff = diff / 1000;

		dayCount = (int) (diff / (24 * 3600));
		diff = diff % (24 * 3600);

		hourCount = (int) (diff / 3600);
		diff = diff % 3600;

		minuteCount = (int) (diff / 60);
		diff = diff % 60;

		secondCount = (int) diff;

		String head = "";
		if (flag == NOT_STARTED) {
			head = "距开始" + (dayCount > 0 ? (int2Str(dayCount) + "天") : "");
		} else if (flag == IN_PROCESS) {
			head = "距结束" + (dayCount > 0 ? (int2Str(dayCount) + "天") : "");
		} else if (flag == ALREAY_FINISHED) {
			head = "已结束";
		}
		
		headText.setText(head);
		hourText.setText(int2Str(hourCount));
		minuteText.setText(int2Str(minuteCount));
		secondText.setText(int2Str(secondCount));
	}

	private void init() {
		startTime = System.currentTimeMillis() - (12 * 3600 * 1000);
		endTime = System.currentTimeMillis() + (12 * 3600 * 1000);
	}

}
